package com.agh.portal.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agh.portal.domain.Student;
import com.agh.portal.domain.security.Role;
import com.agh.portal.domain.security.User;
import com.agh.portal.domain.security.UserRole;
import com.agh.portal.security.CustomUserService;

@Service
@Transactional
public class SecurityUtils {
	
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Autowired CustomUserService userService;
  
    public User createUserAcc(String email, String password){
		User user = new User();
		user.setActivationDate(null);
		user.setEmailAddress(email);
		user.setPassword(passwordEncoder.encode(password));
		user.setLocked(false);
		user.setActivationDate(new Date());
		user.setEnabled(true);
		user.persist();
		
		UserRole userRole = new UserRole();
		userRole.setUser(user);
		userRole.setRole(Role.findRoleByName("ROLE_USER"));
		userRole.persist();
		
		return user;
    }
    
    public User createAdminAcc(String email, String password){
		User user = new User();
		
		user.setEmailAddress(email);
		user.setPassword(passwordEncoder.encode(password));
		user.setLocked(false);
		user.setActivationDate(new Date());
		user.setEnabled(true);
		user.persist();
		
		UserRole userRole = new UserRole();
		userRole.setUser(user);
		userRole.setRole(Role.findRoleByName("ROLE_USER"));
		userRole.persist();
		
		UserRole userRole2 = new UserRole();
		userRole2.setUser(user);
		userRole2.setRole(Role.findRoleByName("ROLE_ADMIN"));
		userRole2.persist();
		
		return user;
    }
    
	public void refreshAuthenticatedUserPrincipal(){
		org.springframework.security.core.userdetails.User currentUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = userService.loadUserByUsername(currentUser.getUsername());
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()));
	}
	
	public boolean isLoggedUserAndStudentMatch(Student student){
		org.springframework.security.core.userdetails.User currentUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return currentUser.getUsername().equals(student.getEmail());
	}
	
	public Student getLoggedStudent(){
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();		
		if(!auth.getName().equals("anonymousUser")){
			return Student.findStudentsByEmailAddress(auth.getName()).getSingleResult();
		}
		return null;
	}
   
}
